package com.asiainfo.fc.data;

import java.util.ArrayList;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.asiainfo.fc.entity.VoteInfo;

public class DataHelper {

	/**
	 * DataHelper
	 */

	public ArrayList<VoteInfo> getVoteList(String strHtml) {
		try {
			Parser parse = new Parser(strHtml);
			parse.setEncoding("gbk");
			NodeList nodeList = parse.parse(new NodeFilter() {

				private static final long serialVersionUID = -8288019527859469658L;

				public boolean accept(Node node) {

					return node.getText().indexOf(
							"tr align='center' bgcolor='#FFFFFF'") != -1;
				}
			});
			ArrayList<VoteInfo> voList = new ArrayList<VoteInfo>();

			for (int i = 0; i < nodeList.size(); i++) {

				Node node = nodeList.elementAt(i);
				NodeList tdList = node.getChildren();
				Node firstNode = tdList.elementAt(0);
				Node secondNode = tdList.elementAt(1);
				if (firstNode != null && secondNode != null) {

					if (secondNode.getChildren().size() == 3
							&& firstNode.getChildren().size() == 1) {

						VoteInfo vo = new VoteInfo();
						vo.setVoteId(Integer.parseInt(firstNode.getChildren()
								.elementAt(0).getText()));

						vo.setVoteNumbers(secondNode.getChildren().elementAt(1)
								.getText().trim());

						voList.add(vo);
					}
				}
			}
			return voList;

		} catch (ParserException e) {

			e.printStackTrace();
			return null;
		}

	}
}
